package cn.itcast.jdbc;


import java.sql.*;

public class jdbcTest {
    public static void main(String[] args) throws SQLException {
        Connection con=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        try {
            //注册驱动,连接 数据库 用户名,密码
            Class.forName("com.mysql.jdbc.Driver");
            String url="jdbc:mysql:///heima01";
            String user="root";
            String password="012346";
            // 获得链接
            con = DriverManager.getConnection(url, user, password);
            String sql="select * from tab_user where id = ? ";
            //获得连接数据库的对象
            ps = con.prepareStatement(sql);
            //设置参数
            ps.setInt(1,1);
            //获得返回数据,并取出数据
            rs = ps.executeQuery();
            while (rs.next()){
                System.out.println( rs.getString("name"));

            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }finally {
            if (rs!=null) {
                rs.close();
            }

        }
        try {
            if (ps!=null) {
                ps.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            if (con!=null) {
                con.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        }


    }



